package com.java.example.app

import android.app.Activity
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.content.Context
import android.os.Handler
import com.java.example.app.Latte.applicationContext
import okhttp3.Interceptor

/**
 * Created by chenll on 2024/8/22 0022.
 *  * 配置文件的存储及获取
 *
 */
class Configurator private constructor() {
    init {
        //配置开始了，但是配置没有完成，所以设置未false
        latteConfigs[ConfigKeys.CONFIG_READY.name] = false
        latteConfigs[ConfigKeys.HANDLER] = HANDLER
    }

    /**
     * 关于这里使用单利模式，要不就是使用双重校验锁，要不就是使用枚举类来进行统一的初始化，还有一种优雅的方式，
     * 静态内部类，静态内部类的单利模式的初始化
     */
    private object Holder {
        val instance: Configurator = Configurator()
    }

    fun configure() {
        latteConfigs[ConfigKeys.CONFIG_READY.name] = true
    }

    /**
     * 根据Pid获取当前进程的名字，一般就是当前app的包名
     *
     * @param pid 进程的id
     * @return 返回进程的名字
     */
    private fun getAppName(pid: Int): String? {
        var processName: String? = null
        val activityManager = applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val list: List<*> = activityManager.runningAppProcesses
        val i = list.iterator()
        while (i.hasNext()) {
            val info = i.next() as RunningAppProcessInfo
            try {
                if (info.pid == pid) {
                    // 根据进程的信息获取当前进程的名字
                    processName = info.processName
                    // 返回当前进程名
                    return processName
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        // 没有匹配的项，返回为null
        return processName
    }

    fun withApiHost(host: String): Configurator {
        latteConfigs[ConfigKeys.API_HOST] = host
        return this
    }

    fun withLoaderDelayed(delayed: Long): Configurator {
        latteConfigs[ConfigKeys.LOADER_DELAYED] = delayed
        return this
    }

    fun withWeChatAppId(appId: String): Configurator {
        latteConfigs[ConfigKeys.WE_CHAT_APP_ID] = appId
        return this
    }

    fun withWeChatAppSecret(appSecret: String): Configurator {
        latteConfigs[ConfigKeys.WE_CHAT_APP_SECRET] = appSecret
        return this
    }

    fun withActivity(activity: Activity): Configurator {
        latteConfigs[ConfigKeys.ACTIVITY] = activity
        return this
    }

    fun withInterceptor(interceptor: Interceptor): Configurator {
        INTERCEPTORS.add(interceptor)
        latteConfigs[ConfigKeys.INTERCEPTOR] = INTERCEPTORS
        return this
    }

    fun withInterceptors(interceptors: ArrayList<Interceptor>?): Configurator {
        INTERCEPTORS.addAll(interceptors!!)
        latteConfigs[ConfigKeys.INTERCEPTOR] = INTERCEPTORS
        return this
    }

    private fun checkConfiguration() {
        val isReady = latteConfigs[ConfigKeys.CONFIG_READY.name] as Boolean
        if (!isReady) {
            throw RuntimeException("Configuration is not ready,call configure")
        }
    }


    fun <T> getConfiguration(key: Any): T? {
        checkConfiguration()
        val value = latteConfigs[key]
        if (value == null) {
            //拦截器不是必须设置
            if (key !== ConfigKeys.INTERCEPTOR) {
                throw NullPointerException("$key IS NULL")
            }
        }
        return latteConfigs[key] as T?
    }

    companion object {
        val latteConfigs: HashMap<Any, Any> = HashMap()
        private val INTERCEPTORS = ArrayList<Interceptor>()
        private val HANDLER = Handler()
        fun getInstance(): Configurator {
            return Holder.instance
        }
    }
}
